#########################################################################################
# 作者：hehung
# 联系：1398660197@qq.com
# 微信：hehung95
# 描述：该脚本必须运行在root权限下，因为keyboard只能在root权限下运行
#       该脚本为手势控制机械臂程序的入口
#########################################################################################
# 修复记录：
#   1. V1.0.0   2023/12/12   基础功能   
#   2. V1.0.1   2023/12/20   手势识别标定
#   3. V1.0.2   2023/12/25   添加标定功能以及键盘控制功能
#   4. V1.0.3   2023/12/31   添加键盘控制机械臂功能   
#########################################################################################

import uart_ctrl
import robotic_arm_gesture as gesture
import robotic_arm_keyboard as kb
import robotic_arm_tuning as tuning


# 文字颜色
BLACK = '\033[30m' 
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'  
BLUE = '\033[34m'
PURPLE = '\033[35m'
CYAN = '\033[36m' 
WHITE = '\033[37m'

BOLD = '\033[1m'

class class_print:
    def __init__(self):
        pass

    # str: 需要打印的文字
    # color: 颜色
    # format: 格式
    def print_color(self, str, color = WHITE):
        print (color + BOLD + str + WHITE)

def print_main_menu():
    app_print.print_color('====================================================================')
    app_print.print_color('=                    Gesture Control Robotic Arm                   =', GREEN)
    app_print.print_color('=                                          -- By hehung            =')
    app_print.print_color('=                    Powered by digikey & eeworld                  =', RED)
    app_print.print_color('=                                                                  =')
    app_print.print_color('= 1. Robotic Arm Control by Gesture                                =')
    app_print.print_color('= 2. Robotic Arm Control by Keyboard                               =')
    app_print.print_color('= 3. Servo Tuning via keyboard                                     =')
    app_print.print_color('= x. Exit                                                          =')
    app_print.print_color('=                                                                  =')
    app_print.print_color('====================================================================')

def print_gesture():
    app_print.print_color('====================================================================', YELLOW)
    app_print.print_color('=  Start Gesture Control                                           =', YELLOW)
    app_print.print_color('=  Description:                                                    =', YELLOW)
    app_print.print_color('=      Press \'ESC\' to Exit                                         =')
    app_print.print_color('====================================================================', YELLOW)

def print_keyboard():
    app_print.print_color('====================================================================', YELLOW)
    app_print.print_color('=  Start Keyboard Control                                          =', YELLOW)
    app_print.print_color('=  Description:                                                    =', YELLOW)
    app_print.print_color('=      Press \'ESC\' to Exit                                       =')
    app_print.print_color('=      Press \'UP\' to control robotic arm up                      =')
    app_print.print_color('=      Press \'DOWN\' to control robotic arm down                  =')
    app_print.print_color('=      Press \'LEFT\' to control robotic arm trun left             =')
    app_print.print_color('=      Press \'RIGHT\' to control robotic arm trun right           =')
    app_print.print_color('=      Press \'W\' to contrl robotic arm to extend forward         =')
    app_print.print_color('=      Press \'S\' to control robotic arm to retract backward      =')
    app_print.print_color('=      Press \'A\' to control robotic clam to trun left            =')
    app_print.print_color('=      Press \'D\' to control robotic clam to trun right           =')
    app_print.print_color('=      Press and hold \'ENTER\' to control robotic clam to grip    =')
    app_print.print_color('=      Release \'ENTER\' to control robotic clam to open           =')
    app_print.print_color('====================================================================', YELLOW)

def print_tuning():
    app_print.print_color('====================================================================', YELLOW)
    app_print.print_color('=  Start servo tuning                                              =', YELLOW)
    app_print.print_color('=  Description:                                                    =', YELLOW)
    app_print.print_color('=      Press \'ESC\' to Exit                                         =')
    app_print.print_color('=      Press \'UP\' to increase the duty cycle                       =')
    app_print.print_color('=      Press \'DOWN\' to decrease the duty cycle                     =')
    app_print.print_color('=      Press \'LEFT\' to switch to the previous servo                =')
    app_print.print_color('=      Press \'RIGHT\' to switch to the next servo                   =')
    app_print.print_color('====================================================================', YELLOW)

if __name__ == '__main__':
    app_print = class_print()
    app_uart = uart_ctrl.class_uart()
    app_gesture = gesture.class_mediapipe()
    app_tuning = tuning.class_tuning()
    app_kb_ctrl = kb.class_kb_ctrl()

    if (app_uart.get_uart_status() == True):
        while True:
            print_main_menu()
            input_text = input()
            if (input_text == '1'):
                print_gesture()
                app_gesture.gesture_main()
                # Start gesture control
            elif (input_text == '2'):
                print_keyboard()
                app_kb_ctrl.kb_ctrl_main()
            elif (input_text == '3'):
                print_tuning()
                app_tuning.tuning_main()
            elif (input_text == 'x' or input_text == 'X'):
                break
            else:
                app_print.print_color('Error Input', RED)
    else:
        app_print.print_color('Serial Opened Failed', RED)

